/*
 * Copyright 2013 Insu Song. All rights reserved.
 * inssong@gmail.com  http://www.kopo.com  
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ARSHAN POURSOHI OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied.
 */
package com.kopo.droidlipokit;

import com.kopo.droidioiolib.Behavior;
import com.kopo.droidioiolib.ControlInterface;
import com.kopo.droidioiolib.LipoKitInterface;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

//This class contains code to read data from the temperature sensor
public class BehaviorSensor extends Behavior {
    private static final String TAG = "kopo.log"; //Log file recording
	private TextView textView1;
	private ToggleButton sv_button;
	
	private float aPin3 = 0;
	private float temp = 0;
	static float roundedTemp = 0;
	
	public BehaviorSensor(View view) {
		super(view);
		Log.d(TAG, "BehaviorSensor created");
	}
	
	
		  
		
	// Called when this behavior is created (maybe again)
	@Override
	public void setup() {
		Log.d(TAG, "BehaviorSensor.setup");

		// Init View
		
		textView1 = (TextView) view.findViewById(R.id.textView2);
		
		
		this.initialized = true;
	}
	

	// Called by ControlInterface when a device is connected
	@Override
	public void loop() {
		//Log.d(TAG, "BehaviorSensor.loop()");

		if( !initialized )
		{
			this.setup();
		}

		
		LipoKitInterface i = (LipoKitInterface)ControlInterface.me;
		
		
		
		i.setLed(false);		//Left this in to test the connection (yellow light)

		// Read analog pins
		aPin3 = i.readIR3();		
		
	    temp = ((aPin3*1024) - 500)/10;
		
		
		roundedTemp = Math.round(temp);
		
		
		
		// Update UI if any
		if( MyControlDevice.currentActivity != null)
			MyControlDevice.currentActivity.runUI(); // invoke uiUpdate in UIThread		
	}

	// called when the behavior is being destroyed
	@Override
	public void cleanup() {
		
	}

	// This is called from a UI thread: either by ItemListActivity or ItemDetailActivity
	@Override
	public void uiUpdate(Context context) {

		// Update pin values
		
		textView1.setText(Float.toString(temp));
		
		
	}
}
